/**
 * 账号登录连接类;
 * 负责账号登录验证;
 */
//import netgamehall from "./../../Hall/Network/GameHallNetwork";
class GLNet {
	CDNCallBack = null;
	uuid = "";
	regName = "";

	//dwc 接口
	//--------------------------注册start------------------------------
	//手机注册
	public registerPhone_fpj(phone,vcode) {
		let client_ip = cc.vv.GLSDKMgr.getClientIp();
		let params = {};
		if (cc.sys.isNative) {
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				params["ip"] = client_ip;
				params["location"] = address;
				cc.vv.GLUtils.getAdress(client_ip, function (address) {
					let params = this.getRegisterOrLogDataFpj("register", address,phone,vcode)
					let referrer = "";
					let promocode = "";
					if (cc.vv.platform.referrer)
						referrer = String(cc.vv.platform.referrer);
					if (cc.vv.platform.promocode)
						promocode = String(cc.vv.platform.promocode)
					params.msg.referrer = referrer
					params.msg.promocode = promocode
					this.netPost(params, this.redisterEnd_fpj.bind(this));
				}.bind(this));
			}.bind(this));
		} else {
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				let params = this.getRegisterOrLogDataFpj("register", address,phone,vcode)
				let referrer = "";
				let promocode = "";
				if (cc.vv.platform.referrer)
					referrer = String(cc.vv.platform.referrer);
				if (cc.vv.platform.promocode)
					promocode = String(cc.vv.platform.promocode)
				params.msg.referrer = referrer
				params.msg.promocode = promocode
				this.netPost(params, this.redisterEnd_fpj.bind(this));
			}.bind(this));
		}
	}

	redisterEnd_fpj(data){
		if (data) {
			var _data = JSON.parse(data);
			if (_data.status) {
				cc.vv.UIMgr.closeAll();
				this.enterGameHall(_data)
			} else {
				cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
				cc.vv.GLUtils.showAlert1(_data.errdesc, function () {
					cc.vv.GLUtils.clearAccountInfo();
					cc.vv.Login.reset();
				}, null);
			}
		}
	}
	getRegisterOrLogDataFpj(command: string, address: string = "",phonenum : string = "",vcode: string = "") {
		let accInfo = cc.vv.GLUtils.getAccountInfo();
		if (!accInfo) {
			return
		}
		let client_ip = cc.vv.GLSDKMgr.getClientIp();
		let sys = "pc";
		if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
			sys = "安卓";
		} else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
			sys = "苹果";
		}
		let model = cc.vv.GLSDKMgr.getSystemModel() //|| "nova50";
		let data = {
			cmd: command,
			msg: {
				plat: "1",
				phone: phonenum,
				vcode:vcode,
				ip: client_ip,
				location: address,
				device_type: sys,
				device_imei: model,
			}
		}
		return data
	}
	//////////////////////////////////////////////////////////////////
	public register(params) {
		if (cc.sys.isNative) {
			let client_ip = cc.vv.GLSDKMgr.getClientIp();
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				params["ip"] = client_ip;
				params["location"] = address;
				this.loginWx(params)
			}.bind(this));
		} else {
			this.loginWx(params)
		}
	}

	getHttpVerificationCode(phoneNum, callBack) {
		let postData = {};
		postData["cmd"] = "VerificationCode"
		postData["msg"] = { phone: phoneNum };

		this.netPost(postData, callBack);
	}

	loginWx(params) {
		let postData = {};
		postData["cmd"] = "login"
		postData["msg"] = params;
		this.netPost(postData, this.loginEnd.bind(this));
	}

	loginXIAOLIAO(params){
		let postData = {};
		postData["cmd"] = "login"
		postData["msg"] = params;
		this.netPost(postData, this.loginEnd.bind(this));
	}


	public registerAccount_dwc(name: string, account: string, password: string): void {
		if (cc.sys.isNative) {
			let client_ip = cc.vv.GLSDKMgr.getClientIp();
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				this.sendRegister_dwc(name, account, password, address)
			}.bind(this));
		} else {
			this.sendRegister_dwc(name, account, password)
		}
	}


	private sendRegister_dwc(name: string, account: string, password: string, address: string = "未知"): void {
		let params = this.getRegisterOrLogData("register", address)
		let referrer = "";
		let promocode = "";
		if (cc.vv.platform.referrer)
			referrer = String(cc.vv.platform.referrer);
		if (cc.vv.platform.promocode)
			promocode = String(cc.vv.platform.promocode)

		params.msg.rolename = name;
		params.msg.role_account = account
		params.msg.login_passwd = password
		params.msg.referrer = referrer
		params.msg.promocode = promocode
		this.netPost(params, this.redisterEnd_dwc.bind(this));
	}

	private redisterEnd_dwc(data): void {
		if (data) {
			var _data = JSON.parse(data);
			if (_data.status) {
				this.enterGameHall(_data)
			} else {
				cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
				cc.vv.GLUtils.showAlert1(_data.errdesc, function () {
					cc.vv.GLUtils.clearAccountInfo();
					cc.vv.Login.reset();
				}, null);
			}
		}

	}
	public login_dwc(loginType: string, account: string = "", password: string = "") {
		if (cc.sys.isNative) {
			let client_ip = cc.vv.GLSDKMgr.getClientIp();
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				this.sendloginReq_dwc(loginType, account, password, address)
			}.bind(this));
		} else {
			this.sendloginReq_dwc(loginType, account, password)
		}
	}

	private sendloginReq_dwc(loginType: string, account: string = "", password: string = "", address = "未知"): void {
		let params = this.getRegisterOrLogData("login", address)
		params.msg.login_type = loginType
		params.msg.role_account = account
		params.msg.login_passwd = password
		this.netPost(params, this.loginEnd.bind(this));
	}

	//--------------------------注册start------------------------------
	public registerAccount(password: string): void {
		if (cc.sys.isNative) {
			let client_ip = cc.vv.GLSDKMgr.getClientIp();
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				this.sendRegister(password, address)
			}.bind(this));
		} else {
			this.sendRegister(password)
		}
	}

	private sendRegister(password: string, address: string = "未知"): void {
		let params = this.getRegisterOrLogData("register", address)
		let referrer = "";
		let promocode = "";
		if (cc.vv.platform.referrer)
			referrer = String(cc.vv.platform.referrer);
		if (cc.vv.platform.promocode)
			promocode = String(cc.vv.platform.promocode)
		params.msg.login_passwd = password
		params.msg.referrer = referrer
		params.msg.promocode = promocode
		this.netPost(params, this.redisterEnd.bind(this));
	}

	private redisterEnd(data): void {
		if (data) {
			var _data = JSON.parse(data);
			if (_data.status) {
				this.enterGameHall(_data)
			} else {
				cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
				cc.vv.GLUtils.showAlert1(_data.errdesc, function () {
					cc.vv.GLUtils.clearAccountInfo();
					cc.vv.Login.reset();
				}, null);
			}
		}

	}

	private getRegisterOrLogData(command: string, address: string = "") {
		let accInfo = cc.vv.GLUtils.getAccountInfo();
		if (!accInfo) {
			return
		}
		let client_ip = cc.vv.GLSDKMgr.getClientIp();
		let sys = "pc";
		if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
			sys = "安卓";
		} else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
			sys = "苹果";
		}
		let model = cc.vv.GLSDKMgr.getSystemModel() //|| "nova50";
		let data = {
			cmd: command,
			msg: {
				plat: "1",
				phone: accInfo.phone_num,
				vcode: accInfo.vcode,
				ip: client_ip,
				location: address,
				device_type: sys,
				device_imei: model,
			}
		}
		return data
	}

	//-------------------------注册end------------------------------------
	private enterGameHall(_data): void {
		let ip = _data.gatesvr.ip
		let port = _data.gatesvr.port
		cc.vv.GLNetDataModel.setGameHallCfg(ip, port);
		cc.vv.GLNetDataModel.setDefaultGameHallCfg(ip, port);
		//保存token
		let accInfo = cc.vv.GLUtils.getAccountInfo();
		accInfo.logintoken = _data.token
		accInfo.serverIP = ip
		accInfo.serverPort = port
		let platFormData = cc.vv.GLNetDataModel.getPlatformCfg()
		accInfo.platform_ip = platFormData.platform_ip
		cc.vv.GLUtils.saveAccountInfo(accInfo);
		new cc.vv.netgamehall();
	}



	login(loginType: string, password: string = "") {
		if (cc.sys.isNative) {
			let client_ip = cc.vv.GLSDKMgr.getClientIp();
			cc.vv.GLUtils.getAdress(client_ip, function (address) {
				this.sendloginReq(loginType, password, address)
			}.bind(this));
		} else {
			this.sendloginReq(loginType, password)
		}
	}

	private sendloginReq(loginType: string, password: string = "", address = "未知"): void {
		let params = this.getRegisterOrLogData("login", address)
		params.msg.login_type = loginType
		params.msg.login_passwd = password
		this.netPost(params, this.loginEnd.bind(this));
	}


	loginEnd(data) {
		console.log("___loginEnd = " + data);
		if (data) {
			var _data = JSON.parse(data);
			if (_data.status) {
				cc.vv.GLConfig.wechatLoginFaildTimes = 0;
				this.enterGameHall(_data);
			} else {
				this.loginFailed(_data.errdesc);
			}
		} else {
			this.loginFailed("");
		}
	}
	loginFailed(str = "") {
		if (str) {
			str = "登录失败," + str;
		} else {
			str = "登录失败!";
		}
		cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
		cc.vv.GLUtils.showAlert1(str, function () {
			cc.vv.GLUtils.clearAccountInfo();
			cc.vv.Login.resetLogin();
		}, null);
	}

	loginEnterGame(): void {
		let accInfo = cc.vv.GLUtils.getAccountInfo();
		if (!accInfo || (!accInfo.serverIP && !accInfo.serverPort)) {
			cc.vv.GLUtils.clearAccountInfo();
			cc.vv.Login.relogin();
			return
		}
		cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path, null, null, "登录中...");
		console.log("____loginEnterGame_____________ip[%d] prot[%d]", accInfo.serverIP, accInfo.serverPort);
		cc.vv.GLNetDataModel.setGameHallCfg(accInfo.serverIP, accInfo.serverPort);
		cc.vv.GLNetDataModel.setDefaultGameHallCfg(accInfo.serverIP, accInfo.serverPort);
		cc.vv.eventCustom.emit("LoginSccess");
		new cc.vv.netgamehall();
	}

	getCdnUrl(callBack) {
		this.CDNCallBack = callBack;
		var _msg = cc.vv.GLConfig.CNDMsg[0];
		if (cc.vv.GLNetDataModel.versionType == 1) {
			_msg = cc.vv.GLConfig.CNDMsg[1];
		}
		var version = cc.vv.GLConfig.getLocalVersion() + "";

		let params = {
			cmd: "getCdnUrl",
			msg: _msg,
			version: version
		};
		this.netPost(params, this.getCdnUrlEnd.bind(this));
	}

	getCdnUrlEnd(data) {
		console.log("___getCdnUrlEnd = " + data);

		if (data) {
			var _data = JSON.parse(data);
			if (_data.err) {
				cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
				let str = _data.err + ",请重试.";
				cc.vv.GLUtils.showAlert1(
					str,
					function () {
						cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path, null, null, "版本检测中...");
						this.getCdnUrl(this.CDNCallBack);
					}.bind(this),
					function () {
						cc.game.end();
					}
				);
			} else {
				//
				cc.vv.GLConfig.setGMURL(_data.res.gw);
				cc.vv.GLConfig.setioshot(_data.res.hotupd);
				cc.vv.GLConfig.setHotUpdateURL(_data.res.cdn);
				if (this.CDNCallBack) {
					this.CDNCallBack();
				}
			}
		} else {
			cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
			let str = "数据获取失败,请重试.";
			cc.vv.GLUtils.showAlert1(
				str,
				function () {
					cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path, null, null, "版本检测中...");
					this.getCdnUrl(this.CDNCallBack);
				}.bind(this),
				function () {
					cc.game.end();
				}
			);
		}
	}

	netPost(data, callBack) {
		var cfg = cc.vv.GLNetDataModel.getPlatformCfg();
		var tar_ip = cfg.platform_ip;
		var tar_port = cfg.platform_port;

		console.log("NetPost data = " + JSON.stringify(data));

		var url = "http://" + tar_ip + ":" + tar_port;
		cc.vv.GLHttpUtils.httpPost1(url, data, callBack);
	}
}
export let glNet: GLNet = new GLNet();
